/**
* ClassName: OrderList
* Package: src/views
* Description: 订单列表页面
*
* @Author Liu Wenrui
* @Create 2025/9/24 16:00
* @Version 1.0
*/

<template>
    <div class="wrapper">
        <!-- header部分 -->
        <header>
            <p>我的订单</p>
        </header>
        <!-- 订单列表部分 -->
        <h3>未支付订单信息：</h3>
        <ul class="order">
            <li v-for="item in unpaidOrderArr" :key="item.id" class="order-card" @click="showDetail(item)">
                <div class="order-main">
                    <span class="order-id">订单号: {{ item.id }}</span>
                    <span class="order-price">总价: &#165;{{ formatPrice(item.orderTotal) }}</span>
                    <span class="order-status paid">未支付</span>
                </div>
            </li>
        </ul>

        <h3>已支付订单信息：</h3>
        <ul class="order">
            <li v-for="item in paidOrderArr" :key="item.id" class="order-card" @click="showDetail(item)">
                <div class="order-main">
                    <span class="order-id">订单号: {{ item.id }}</span>
                    <span class="order-price">总价: &#165;{{ formatPrice(item.orderTotal) }}</span>
                    <span class="order-status paid">已支付</span>
                </div>
            </li>
        </ul>

        <div v-if="showDialog" class="order-dialog-mask" @click.self="closeDialog">
            <div class="order-dialog">
                <h3>订单详情</h3>
                <div class="order-dialog-content">
                    <div class="order-detail-row">
                        <span class="order-detail-label">订单号：</span>
                        <span>{{ currentOrder.id }}</span>
                    </div>
                    <div class="order-detail-row">
                        <span class="order-detail-label">总价：</span>
                        <span>￥{{ formatPrice(currentOrder.orderTotal) }}</span>
                    </div>
                    <ul v-if="currentOrder.orderDetailetList && currentOrder.orderDetailetList.length"
                        class="order-detail-list">
                        <li>
                            <span>送货地址: {{ currentOrder.deliveryAddress ? currentOrder.deliveryAddress.address : '无'
                            }}</span>
                        </li>
                        <li>
                            <span>收货人: {{ currentOrder.deliveryAddress ? currentOrder.deliveryAddress.contactName : ''
                            }}</span>
                        </li>
                        <li>
                            <span>电话: {{ currentOrder.deliveryAddress ? currentOrder.deliveryAddress.contactTel : ''
                            }}</span>
                        </li>
                        <li>
                            <span>送货地址: {{ currentOrder.deliveryAddress ? currentOrder.deliveryAddress.address : '无'
                            }}</span>
                        </li>
                    </ul>
                    <button @click="closeDialog" class="dialog-btn">关闭</button>
                </div>
            </div>
        </div>
        <!-- 底部菜单部分 -->
        <Footer></Footer>

    </div>
</template>
<script>
import Footer from '../components/Footer.vue';
export default {
    name: 'OrderList',
    data() {
        return {
            unpaidOrderArr: [],
            paidOrderArr: [],
            user: {},
            showDialog: false,
            currentOrder: {}
        }
    },
    created() {
        this.$axios.get("/user")
            .then(response => {
                this.user = response.data;
            });
        // 未支付订单
        this.$axios.get('/orders/by-status', { params: { orderStatus: 0 } })
            .then(response => {
                let result = response.data.data;
                result = result.filter(order => order.customerId === this.user.id);
                for (let orders of result) {
                    orders.isShowDetailet = false;
                }
                this.unpaidOrderArr = result;
            });
        // 已支付订单
        this.$axios.get('/orders/by-status', { params: { orderStatus: 1 } })
            .then(response => {
                let result = response.data.data;
                console.log('Paid orders:', result);
                result = result.filter(order => order.customerId === this.user.id);
                for (let orders of result) {
                    orders.isShowDetailet = false;
                }
                this.paidOrderArr = result;
            });
    },
    methods: {
        detailetShow(orders) {
            orders.isShowDetailet = !orders.isShowDetailet;
        },
        formatPrice(price) {
            if (price == null || price == undefined || price === '') return '0.00';
            return parseFloat(price).toFixed(2);
        },
        showDetail(order) {
            console.log('Show detail for order:', order);
            this.currentOrder = order;
            this.showDialog = true;
            this.$axios.get(`/addresses/${order.addressId}`)
                .then(response => {
                    // 将查询到的地址信息赋值到 currentOrder.deliveryAddress
                    this.$set(this.currentOrder, 'deliveryAddress', response.data.data);
                });
        },
        closeDialog() {
            this.showDialog = false;
            this.currentOrder = {};
        },
    },

    components: {
        Footer
    }
}
</script>
<style scoped>
/****************** 总容器 ******************/
.wrapper {
    width: 100%;
    height: 100%;
}

/****************** header部分 ******************/
.wrapper header {
    width: 100%;
    height: 12vw;
    background-color: #0097FF;
    color: #fff;
    font-size: 4.8vw;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
}

/****************** 历史订单列表部分 ******************/
.wrapper h3 {
    margin-top: 12vw;
    box-sizing: border-box;
    padding: 4vw;
    font-size: 4vw;
    font-weight: 300;
    color: #999;
}

.wrapper .order {
    width: 100%;
}

.wrapper .order li {
    width: 100%;
}

.wrapper .order li .order-info {
    box-sizing: border-box;
    padding: 2vw 4vw;
    font-size: 4vw;
    color: #666;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.wrapper .order li .order-info .order-info-right {
    display: flex;
}

.wrapper .order li .order-info .order-info-right .order-info-right-icon {
    background-color: #f90;
    color: #fff;
    border-radius: 3px;
    margin-left: 2vw;
    user-select: none;
    cursor: pointer;
}

.wrapper .order li .order-detailet {
    width: 100%;
}

.wrapper .order li .order-detailet li {
    width: 100%;
    box-sizing: border-box;
    padding: 1vw 4vw;
    color: #666;
    font-size: 3vw;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.order-card {
    margin: 2vw 0;
    padding: 2vw 3vw;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 8px #eee;
}

.order-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 4vw;
}

.order-id {
    font-weight: bold;
    color: #0097FF;
    font-size: 4.2vw;
    margin-right: 2vw;
}

.order-price {
    color: #38CA73;
    font-size: 4vw;
    font-weight: bold;
}

.order-status-box {
    display: flex;
    align-items: center;
    gap: 2vw;
}

.order-status.paid {
    color: #38CA73;
    font-weight: bold;
    font-size: 4vw;
    background: #eafbe7;
    border-radius: 6px;
    padding: 0.5vw 2vw;
}

.order-date {
    color: #999;
    font-size: 3vw;
}

.order-toggle {
    cursor: pointer;
    margin-left: 1vw;
}

.order-dialog-mask {
    position: fixed;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.order-dialog {
    background: #fff;
    border-radius: 12px;
    padding: 32px 24px;
    min-width: 320px;
    max-width: 90vw;
    box-shadow: 0 4px 24px #aaa;
}

.order-dialog h3 {
    margin-bottom: 16px;
    font-size: 20px;
    color: #0097FF;
    text-align: center;
}

.order-dialog-content {
    margin: 0 0 12px 0;
    font-size: 16px;
    color: #333;
    line-height: 2;
    padding: 0 12px;
}

.order-detail-row {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
}

.order-detail-label {
    color: #999;
    min-width: 80px;
    text-align: right;
    margin-right: 8px;
}

.order-dialog-status.paid {
    color: #38CA73;
    font-weight: bold;
}

.order-dialog-status.unpaid {
    color: #f90;
    font-weight: bold;
}

.order-detail-list {
    margin-top: 10px;
    padding-left: 0;
}

.order-detail-list li {
    font-size: 15px;
    color: #555;
    margin-bottom: 6px;
    display: flex;
    justify-content: space-between;
}

.dialog-btn {
    margin-top: 18px;
    background: #0097FF;
    color: #fff;
    border: none;
    padding: 8px 28px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 16px;
}
</style>